package com.ruoyi.quartz.task;

import com.ruoyi.common.utils.HttpClientHelper;
import com.ruoyi.system.domain.WxToken;
import com.ruoyi.system.service.IWxTokenService;
import com.ruoyi.web.controller.config.WxConfig;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ruoyi.common.utils.StringUtils;

/**
 * 定时任务调度测试
 * 
 * @author ruoyi
 */
@Component("ryTask")
public class RyTask
{
    private static Logger logger = LoggerFactory.getLogger("sys-task");

    @Autowired
    private IWxTokenService iWxTokenService;

    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
    {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params)
    {
        System.out.println("执行有参方法：" + params);
    }

    // 定时获取accessToken
    public void ryNoParams()
    {

        String url ="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+ WxConfig.APPID+"&secret="+WxConfig.APP_SECRET+"";
        String result = HttpClientHelper.doGet(url);
        JSONObject resultJson = JSONObject.fromObject(result);
        String accessToken = resultJson.get("access_token").toString();

        WxToken wxToken = new WxToken();

        String urlStr = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+accessToken+"&type=jsapi";
        String backData=HttpClientHelper.doGet(urlStr);
        String ticket = (String) JSONObject.fromObject(backData).get("ticket");
        if(StringUtils.isNotEmpty(ticket)){
            wxToken.setTicket(ticket);
            wxToken.setAccessToken(accessToken);
            iWxTokenService.deleteToken();
            iWxTokenService.insertToken(wxToken);

        }else{
            logger.info("ryNoParams() get AccessToken:{}");
            return;
        }



//        wxToken.setAccessToken(accessToken);
//        iWxTokenService.deleteToken();
//        iWxTokenService.insertToken(wxToken);
    }
}
